package com.eboss2.sbs.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.ActivityServiceGoodsOrderDetail;
import com.eboss2.sbs.interfaces.ServiceGoodsOrderCallbackListener;
import com.eboss2.sbs.model.ServiceGoodsListInfo;
import com.eboss2.sbs.model.ServiceGoodsOrderInfo;
import com.eboss2.sbs.weight.DialogUtils;
import com.tool.utils.utils.StringUtils;
import com.tool.utils.view.MyListView;
import com.tool.utils.view.RTextView;

import java.util.ArrayList;
import java.util.List;

public class ServiceGoodsOrderListManagerAdapter extends BaseAdapter {

	private Context context;
	private List<ServiceGoodsOrderInfo> datas;
	private LayoutInflater inflater;
	private ViewHolder holder = null;
	private ServiceGoodsOrderCallbackListener listener;

	public ServiceGoodsOrderListManagerAdapter(Context context, List<ServiceGoodsOrderInfo> datas, ServiceGoodsOrderCallbackListener listener) {
		this.context = context;
		this.datas = datas;
		this.inflater = LayoutInflater.from(context);
		this.listener = listener;
	}

	@Override
	public int getCount() {
		if(datas!=null&&datas.size()>0)return datas.size();
		else return 0;
	}

	@Override
	public ServiceGoodsOrderInfo getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		


		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_service_order_item, null);
			
			holder.tv_order_no = (TextView) convertView.findViewById(R.id.tv_order_no);
			holder.tv_order_time = (TextView) convertView.findViewById(R.id.tv_order_time);
			holder.ll_spec = (LinearLayout) convertView.findViewById(R.id.ll_spec);
			holder.listview_spec = (MyListView) convertView.findViewById(R.id.listview_spec);
			holder.tv_order_num = (TextView) convertView.findViewById(R.id.tv_order_num);
			holder.tv_order_amt = (TextView) convertView.findViewById(R.id.tv_order_amt);
			holder.tv_member_name = (TextView) convertView.findViewById(R.id.tv_member_name);
			holder.tv_more = (TextView) convertView.findViewById(R.id.tv_more);
			holder.tv_phone = (RTextView) convertView.findViewById(R.id.tv_phone);
			holder.tv_btn_3 = (SuperButton) convertView.findViewById(R.id.tv_btn_3);
			holder.tv_btn_2 = (SuperButton) convertView.findViewById(R.id.tv_btn_2);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.tv_btn_3.setVisibility(View.GONE);
		holder.tv_btn_2.setVisibility(View.GONE);




		final ServiceGoodsOrderInfo data = datas.get(position);

		holder.tv_order_no.setText(StringUtils.isBlank(data.getOrder_no()) ? "" : "订单号: "+ data.getOrder_no());
		holder.tv_order_time.setText(StringUtils.isBlank(data.getOrder_time()) ? "" : "订单时间: "+ data.getOrder_time());
		holder.tv_order_num.setText("共"+data.getOrder_num()+"件商品");
		holder.tv_order_amt.setText(StringUtils.formatIntMoney(data.getOrder_amt()));
		holder.tv_member_name.setText(StringUtils.isBlank(data.getMemberName()) ? "" : data.getMemberName());
		holder.tv_phone.setText(StringUtils.isBlank(data.getShopBooKingMessage().getPhone()) ? "" : data.getShopBooKingMessage().getPhone());
		holder.tv_phone.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
//						final String phone = holder.tv_phone.getText().toString().trim();
				DialogUtils.alert("拨打 " + data.getShopBooKingMessage().getPhone(), null, context, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.getShopBooKingMessage().getPhone()));
						// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						context.startActivity(intent);
					}
				}, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();

					}
				});

			}
		});


		if (data.getIs_verify() == 0){
			holder.tv_btn_2.setVisibility(View.VISIBLE);
			holder.tv_btn_3.setVisibility(View.VISIBLE);
			holder.tv_btn_3.setText("确认预约");
			holder.tv_btn_3.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					listener.commit(data.getOrder_id(), data.getShopBooKingMessage().getMemberId(), 1, data.getOrder_no());
				}
			});
			holder.tv_btn_2.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					listener.commit(data.getOrder_id(), data.getShopBooKingMessage().getMemberId(),2, data.getOrder_no());
				}
			});
		}else if (data.getIs_verify() == 1){
			holder.tv_btn_2.setVisibility(View.GONE);
			holder.tv_btn_3.setVisibility(View.VISIBLE);
			holder.tv_btn_3.setText("取消预约");
			holder.tv_btn_3.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					listener.refund(data.getOrder_id(), data.getShopBooKingMessage().getMemberId(), data.getOrder_no());
				}
			});
		}


		//商品列表
		if(data.getGood_list() != null && data.getGood_list().size() > 0){

			if (data.getGood_list().size() > 2) {
				List<ServiceGoodsListInfo> serviceGoodsListInfos = new ArrayList<>();
				serviceGoodsListInfos.add(data.getGood_list().get(0));
				serviceGoodsListInfos.add(data.getGood_list().get(1));
				holder.listview_spec.setAdapter(new ServiceOrderListAdapter(context, serviceGoodsListInfos));
				holder.tv_more.setVisibility(View.VISIBLE);
			}else{
				holder.listview_spec.setAdapter(new ServiceOrderListAdapter(context, data.getGood_list()));
				holder.tv_more.setVisibility(View.GONE);
			}


			holder.listview_spec.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
					context.startActivity(new Intent(context, ActivityServiceGoodsOrderDetail.class).putExtra("data", data));
				}
			});

		}else{
			holder.listview_spec.setVisibility(View.GONE);
		}



		return convertView;
	}





	public final class ViewHolder {
		private TextView tv_order_no;
		private TextView tv_order_time;
		private LinearLayout ll_spec;
		private MyListView listview_spec;
		private TextView tv_order_num;
		private TextView tv_order_amt;
		private TextView tv_member_name;
		private TextView tv_more;
		private RTextView tv_phone;
		private SuperButton tv_btn_2;
		private SuperButton tv_btn_3;
	}

}
